Skip to content

fix(napi): receive raw pointer out parameters#57

Open
GrapeBaBa wants to merge 5 commits into
mainfrom
fix/napi-out-parameters-v2
Open

fix(napi): receive raw pointer out parameters#57
GrapeBaBa wants to merge 5 commits into
mainfrom
fix/napi-out-parameters-v2

Conversation

@GrapeBaBa

@GrapeBaBa GrapeBaBa commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Motivation

Node-API returns several values through pointer-sized C out parameters. The previous bindings passed addresses of higher-level Zig struct or wrapper storage to some of these APIs, so Node-API wrote raw pointer or handle bits into the wrong fields instead of a correctly typed raw slot. This could corrupt returned Node version data and TypedArray or DataView backing ArrayBuffer wrappers. Receiving the exact C values first, then copying or wrapping them only after a successful call, preserves both the C ABI and the Zig wrapper invariants. Unsupported TypedArray tags must likewise become a normal conversion error rather than an enum-conversion trap.

Summary

  • receive the Node version pointer through its own raw out slot before copying the version structure
  • receive TypedArray and DataView backing ArrayBuffer handles as raw napi_value values
  • construct Value wrappers only after successful Node-API calls
  • report unsupported Node-API TypedArray element types as UnsupportedTypedarrayType instead of trapping during enum conversion

@GrapeBaBa
GrapeBaBa marked this pull request as ready for review July 20, 2026 12:33
Comment thread examples/js_dsl/mod.test.ts Outdated
Comment on lines +293 to +295
it("rejects unsupported TypedArray element types", () => {
const Float16ArrayCtor = Reflect.get(globalThis, "Float16Array");
if (typeof Float16ArrayCtor !== "function") return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that condition does not match, it will show the tests pass not skipped. We can use something like this to properly show the test skipped.

it.skipIf(typeof Float16Array !== "function")

Comment thread src/Value.zig
/// https://nodejs.org/api/n-api.html#napi_get_typedarray_info
pub fn getTypedarrayInfo(self: Value) NapiError!TypedarrayInfo {
var info: TypedarrayInfo = undefined;
pub fn getTypedarrayInfo(self: Value) TypedarrayInfoError!TypedarrayInfo {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note for reference. The error set change on a pub function, will be breaking for usage when user completely exhausting error union.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is what I thought a while and finally add a new error

nazarhussain
nazarhussain previously approved these changes Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants